package com.example.a053checkbox_switch_radiobutton_imageview;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

public class SwitchActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    private TextView mTextView;
    private Switch mSwitch1;
    private Switch mSwitch2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_switch);
        initView();
    }

    private void initView() {
        mTextView = findViewById(R.id.textView);
        mSwitch1 = findViewById(R.id.switch1);
        mSwitch1.setOnCheckedChangeListener(this);
        mSwitch2 = findViewById(R.id.switch2);
        mSwitch2.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        String strMsg = "";
        if (b) {
            strMsg = String.format("控件:%s ; ID:%d; 被选中", compoundButton.getText(), compoundButton.getId());
        } else {
            strMsg = String.format("控件:%s; ID:%d; 被取消", compoundButton.getText(), compoundButton.getId());
        }
        mTextView.setText(strMsg);
    }
}